/*
 * plmpwm.c
 *
 *  Created on: May 4, 2018
 *      Author: yuanjianen
 */


#include "comath.h"
#include "plpwm.h"
#include "plpwm_config.h"
#include "plconfig.h"

void plpwm_init_pwm(void) {
	uint8 index;
	const PlPwmConfig *config;

	for(index = 0U; index < plpwm_pwm_config_count; index++) {
		config = &(plpwm_pwm_config[index]);
		if(config->config_index_ref != NULL) {
			*(config->config_index_ref) = index;
		}

		plpwm_set_duty(index,config->pwm_init_duty);
	}
}
void plpwm_set_duty(uint8 config_index, uint16 duty) {

	const PlPwmConfig *config;
	uint32 master_dcr_value = 0;

	config = &(plpwm_pwm_config[config_index]);
	master_dcr_value = plpwm_get_channel_tdr(config->master_channel) + 1;
	if(config_index < plpwm_pwm_config_count) {
		if( duty > 0u && duty < 100u){
			plpwm_set_channel_tdr(config->slave_channel,(uint16)(master_dcr_value*duty/100));
		}else if(duty <= 0u){
			plpwm_set_channel_tdr(config->slave_channel,0);
		}else if(duty >= 100u){
			plpwm_set_channel_tdr(config->slave_channel,master_dcr_value);
		}else{
		}
	}
}
